/**
 * 
 */
package com.scpii.universal.service.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.scpii.universal.constant.GenericConstants;
import com.scpii.universal.hibernate.dao.AttributeListDao;
import com.scpii.universal.hibernate.entity.AttributeListTab;
import com.scpii.universal.service.AttributeListService;

/**
 * genericWeb
 * @author weny
 * 2012-7-8下午09:35:48
 */
@Service
public class AttributeListServiceImpl extends BaseServiceImpl<AttributeListTab, Integer> implements AttributeListService {
	@Resource
	private AttributeListDao attributeListDao;
	private static final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	
	@Resource
	public void setBaseDao(AttributeListDao attributeListDao) {
		super.setBaseDao(attributeListDao);
	}

	@Override
	public List<AttributeListTab> getAttributeListByAttrType(int attrType) {
//		StringBuffer hql = new StringBuffer(" SELECT * FROM ATTRIBUTE_LIST_TAB t ");
//		hql.append(" WHERE t.INT_ATTR_TYPE=:intAttrType ");
//		hql.append(" AND t.INT_DELETE_SIGN=:deleteSign ");
		
		StringBuffer hql = new StringBuffer("FROM com.scpii.universal.hibernate.entity.AttributeListTab t");
		hql.append(" WHERE t.intAttrType=:intAttrType ");
		hql.append(" AND t.intDeleteSign=:deleteSign");
		hql.append(" ORDER BY t.intAttrListId ASC");
		
		Map<String, Object> argsMap = new HashMap<String, Object>();
		argsMap.put("intAttrType", attrType);
		argsMap.put("deleteSign", GenericConstants.DELETE_SIGN_EXIST);
		
		List<AttributeListTab> attrListTabList = attributeListDao.hqlGetList(hql.toString(), argsMap);
		
		return attrListTabList;
	}

	@Override
	public void saveOptionsBatch(int type, String[] options) {
		String date = sdf.format(new Date());
		Integer maxId = attributeListDao.getMaxId();
		for (int i = 0; i < options.length; i++) {
			maxId ++ ;
			AttributeListTab attributeListTab = new AttributeListTab();
			attributeListTab.setIntDeleteSign(GenericConstants.DELETE_SIGN_EXIST);
			attributeListTab.setIntAttrListKey((short) (i+1));
			attributeListTab.setIntAttrType(type);
			attributeListTab.setVarAttrListValue(options[i]);
			attributeListTab.setDatUpdateDatetime(date);
			attributeListTab.setIntAttrListId(maxId);
			attributeListDao.save(attributeListTab);
		}
	}
}
